<template>
  <el-dialog
    title="用户详情"
    :close-on-click-modal="false"
    :visible.sync="visible"
    style="min-width: 1400px;">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px" label-position="right">
      <div style="display: flex;">
        <div
          style="text-align: center;flex: 1;display: flex;flex-direction: column;align-items: center;justify-content: center;">
          <el-upload
            ref="uploadHeadImg"
            :action="uploadHeadImgUrl"
            :on-success="handleHeadImgSuccess"
            :on-error="handleHeadImgError"
            :show-file-list="true"
            :before-upload="beforeHeadImgUpload">
            <img v-if="dataForm.pbUser.avatar" :src="dataForm.pbUser.avatar" class="headImg">
            <i v-else class="el-icon-plus headImg"></i>
          </el-upload>
          <span style="margin-top: 20px;display: block">修改头像</span>
        </div>
        <div style="flex: 1;">
          <el-form-item label="手机号码：" prop="pbUser.phone" style="width: 240px;">
            <el-input v-model="dataForm.pbUser.phone" placeholder="手机号码"></el-input>
          </el-form-item>
          <el-form-item label="微信号：">
          </el-form-item>
          <el-form-item label="性别：" prop="pbUser.gender" style="width: 240px;">
            <el-select v-model="dataForm.pbUser.gender">
              <el-option
                v-for="item in genderOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="宝宝昵称：" prop="pbChild.nickName" style="width: 240px;" v-if="dataForm.pbChild">
            <el-input v-model="dataForm.pbChild.nickName" placeholder="宝宝昵称"></el-input>
          </el-form-item>
          <el-form-item label="宝宝性别：" prop="pbChild.gender" style="width: 240px;" v-if="dataForm.pbChild">
            <el-select v-model="dataForm.pbChild.gender">
              <el-option
                v-for="item in genderChildOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="制药VIP：">
            {{dataForm.pbUser.vip==0?'非VIP会员':'VIP会员'}}
          </el-form-item>
          <el-form-item label="注册时间：">
            {{dataForm.pbUser.joinTime?utils.formatUnixtimestamp(dataForm.pbUser.joinTime):''}}
          </el-form-item>
        </div>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        uploadHeadImgUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        dataForm: {
          'pbChild': {
            'avatar': '',
            'birthTime': '',
            'createTime': '',
            'delFlag': 0,
            'enable': 0,
            'gender': 0,
            'id': 0,
            'nickName': '',
            'pbUserId': 0,
            'remark': '',
            'updateTime': ''
          },
          'pbUser': {
            'avatar': '',
            'createTime': '',
            'delFlag': 0,
            'gender': 0,
            'genderCn': '',
            'id': 0,
            'imei': '',
            'joinTime': '',
            'nickName': '',
            'phone': '',
            'remark': '',
            'status': 0,
            'updateTime': '',
            'vip': 0
          }
        },
        genderOptions: [
          {id: 0, label: '未知'},
          {id: 1, label: '男'},
          {id: 2, label: '女'},
        ],
        genderChildOptions: [
          {id: 0, label: '未知'},
          {id: 1, label: '男'},
          {id: 2, label: '女'},
        ],
        dataRule: {
          'pbUser.phone': [
            {required: true, trigger: 'blur', message: '手机号不能为空'}
          ],
          'pbUser.gender': [
            {required: true, message: '性别不能为空', trigger: 'blur'}
          ],
          'pbChild.nickName': [
            {required: true, message: '宝宝昵称不能为空', trigger: 'blur'}
          ],
          'pbChild.gender': [
            {required: true, message: '宝宝性别不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.pbUser.avatar = ''
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        // if (!this.dataForm.pbUser.avatar) {
        //   this.$message.error('请上传头像')
        // }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(!this.dataForm.pbChild){
              delete this.dataForm.pbChild
            }
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbuser/update`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleHeadImgSuccess (res, file) {
        this.dataForm.pbUser.avatar = res.url
        let _this = this
        setTimeout(function () {
          _this.$refs.uploadHeadImg.clearFiles()
        }, 1000)
      },
      beforeHeadImgUpload (file) {
        if (file.type.indexOf('image') < 0) {
          this.$message.error('图片格式不正确')
          return false
        }
      },
      handleHeadImgError (err, file, fileList) {
        this.$message.error('上传失败')
      }
    }
  }
</script>
<style lang="less" scoped>
  .headImg {
    color: #8c939d;
    border: 1px solid #d9d9d9;
    border-radius: 50%;
    width: 240px;
    height: 240px;
    line-height: 240px;
    font-size: 28px;
  }
</style>
